package interview.javabase.thread.threethreadprint;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Collections;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
public class PrintDemo implements Runnable {

    private String msg;
    private int count;
    private static int ai;

    private static Lock lock = new ReentrantLock();

    public PrintDemo(String msg, int count) {
        this.msg = msg;
        this.count = count;
    }

    @Override
    public void run() {
        while (ai < 30) {
            try {
                lock.lock();
                while (ai < 30 && count == ai % 3) {
                    System.out.print(msg);
                    ai++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        new Thread(new PrintDemo("A", 0)).start();
        new Thread(new PrintDemo("B", 1)).start();
        new Thread(new PrintDemo("C", 2)).start();
    }
}

































/*
 private String name;
    private int count;

    public PrintDemo(String name, int count) {
        this.name =  name;
        this.count = count;
    }

    private static Lock lock = new ReentrantLock();
    private static int currentCount;

    @Override
    public void run() {
        while (currentCount < 30) {
            try{
                lock.lock();
                while (count == currentCount%3 && currentCount < 30) {
                    System.out.print(name);
                    currentCount++;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }*/